package com.syni.mdd.yhd.common.all.controller.protect;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dto.requestDto.api.PushDto;
import com.syni.mdd.yhd.common.all.service.BmsDxNewsService;
import com.syni.mdd.yhd.common.all.service.api.AuroraPushService;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddDspApi.CreateNewsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/dxNews")
public class BmsDxNewsController{
    @Autowired
    BmsDxNewsService bmsDxNewsService;
    @Autowired
    AuroraPushService AuroraPushService;

    @PostMapping("/getUserNews")
    @CheckParams(checkAuth = true)
    public Map<String,Object> getNews(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        return bmsDxNewsService.findTopOnceType(userId);
    }

    @PostMapping("/getUserNewsByType")
    @CheckParams(checkAuth = true,params = {"news_type","page_num","page_size"})
    public Map<String,Object> getUserNewsByType(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        Integer newsType = Integer.parseInt(paramsMap.get("news_type").toString());
        return bmsDxNewsService.findUserClientNews(userId,newsType,pageNum,pageSize);
    }

    @PostMapping("/getBusinessNews")
    @CheckParams(checkAuth = true,params = {"businessId"})
    public Map<String,Object> getBusinessNews(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("businessId").toString());
        return bmsDxNewsService.findBusinessTopNew(userId,businessId);
    }

    @PostMapping("/getBusinessNewsByType")
    @CheckParams(checkAuth = true,params = {"business_id","news_type","page_num","page_size"})
    public Map<String,Object> getBusinessNewsByType(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        Integer newsType = Integer.parseInt(paramsMap.get("news_type").toString());
        return bmsDxNewsService.findBusinessClientNews(userId,businessId,newsType,pageNum,pageSize);
    }

    @PostMapping("/fxCenterNews")
    public Map<String,Object> fxCenterNews(@RequestBody CreateNewsDto createNewsDto){
        Map<String, Object> fxNews = bmsDxNewsService.createFxNews(createNewsDto);
        if(fxNews.get("status") != null && StringUtils.equals(fxNews.get("status").toString(),"00")){
            Integer newId = Integer.parseInt(String.valueOf(fxNews.get("data")));
            PushDto pushDto = new PushDto();
            pushDto.setNewsId(newId);
            pushDto.setCode(null);
            return AuroraPushService.pushNews(pushDto);
        }else{
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }
}
